package com.tbit.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by John on 2016/12/21.
 */
public class DateUtil {
    /**
     * 一天的毫秒数
     */
    public static final Integer DAY_MS = 24 * 60 * 60 * 1000;

    public static Calendar deaCal;
    public static DateUtil YMDHMS_ = new DateUtil("yyyy-MM-dd HH:mm:ss");
    public static DateUtil MDHMS_ = new DateUtil("MM-dd HH:mm:ss");
    public static DateUtil YMD_ = new DateUtil("yyyy-MM-dd");
    public static DateUtil YMD = new DateUtil("yyyyMMdd");
    public static DateUtil YM = new DateUtil("yyyy-MM");
    public static DateUtil yyyyMMddHHmmss = new DateUtil("yyyyMMddHHmmss");

    static {
        deaCal = Calendar.getInstance();
        deaCal.set(2009, 1, 1);
    }

    private String date_format;
    private ThreadLocal threadlocal;

    public DateUtil(String dateformat) {
        this.date_format = dateformat;
        threadlocal = new ThreadLocal();
    }

    /**
     * 获取用户时区的本地的当天的开始时间
     *
     * @param timezone 用户所在的时区
     * @return
     */
    public static long getDate(int timezone) {
        ZoneId zoneId = ZoneId.of(mins2TimeZoneId(timezone));
        LocalDate ld = LocalDate.now(zoneId);
        return ld.atStartOfDay(zoneId).toEpochSecond() * 1000;
    }

    /**
     * 获取去年的今天
     *
     * @param timezone
     * @return
     */
    public static long getLastYearDate(int timezone) {
        ZoneId zoneId = ZoneId.of(mins2TimeZoneId(timezone));
        LocalDate ld = LocalDate.now(zoneId).plusYears(-1);
        return ld.atStartOfDay(zoneId).toEpochSecond() * 1000;
    }

    /**
     * 获取本周的开始时间
     *
     * @param timezone
     * @return
     */
    public static long getWeek(int timezone) {
        ZoneId zoneId = ZoneId.of(mins2TimeZoneId(timezone));
        LocalDate ld = LocalDate.now(zoneId);
        int value = ld.getDayOfWeek().getValue();
        return ld.atStartOfDay(zoneId).toEpochSecond() * 1000 - (DateUtil.DAY_MS * (value - 1));
    }

    public static String mins2TimeZoneId(int mins) {
        int value = Math.abs(mins);
        int hour = value / 60;
        int min = value % 60;
        return String.format("GMT%s%02d:%02d", mins >= 0 ? "+" : "-", hour, min);
    }

    public DateFormat getdateformat() {
        Object obj = threadlocal.get();
        if (obj == null) {
            obj = getobj();
        }
        return (DateFormat) obj;
    }

    private synchronized Object getobj() {
        Object obj = threadlocal.get();
        if (null == obj) {
            obj = new SimpleDateFormat(date_format);
            threadlocal.set(obj);
        }
        return obj;
    }

    public Date parse(String textdate) throws ParseException {
        return getdateformat().parse(textdate);
    }

    public String format(Date dt) {
        if (null == dt) {
            return null;
        }
        return getdateformat().format(dt);
    }

    public String format(Long dt) {
        return getdateformat().format(dt);
    }
}
